<?php
namespace App\Api\Controllers\V1;
use App\Services\CardService;
use App\Api\Controllers\Validators\ClockValidator;
use App\Api\Helper\ReturnMessage;
use App\Models\api\Clock;
use App\Services\ClocksService;
use Illuminate\Http\Request;

class ClockController extends BaseController
{

	/**
	 * @param Request $request
	 * @return \Dingo\Api\Http\Response
	 * @name 打卡系统处理
	 * @user IHammer
	 */
	public static function clockRecord(Request $request){
		$res = ClocksService::clockRecordHandle($request);
		return ReturnMessage::successToAppInfo($res);
	}
	/**
	 * @param Request $request
	 * @return \Dingo\Api\Http\Response
	 * @name 员工打卡
	 * @user IHammer
	 */
	public static function clocksCreate(Request $request){
		$user_id = constant("_USER_ID");
		$company_id = constant("_USER_CORPORATION_ID");
		$request->offsetSet('user_id',$user_id);
		$request->offsetSet('company_id',$company_id);
		$res = ClockValidator::create($request);
		$errmsg = "打卡失败。";
		$res = ClocksService::clocksCreateHandle($res,$errmsg);
		if($res){
			return ReturnMessage::success('打卡成功 - 问答');
		}
		return ReturnMessage::success($errmsg,1001);
	}

	/**
	 * @param Request $request
	 * @return \Dingo\Api\Http\Response
	 * @name 打卡列表
	 * @user IHammer
	 */
	public static function clocksLists(Request $request){
		$request->offsetSet('user_id',constant("_USER_ID"));
		$request->offsetSet('company_id',constant("_USER_CORPORATION_ID"));
		$request->offsetSet('user_role',constant('_USER_ROLE'));
		$res = ClocksService::getClocksListsHandle($request);
		return ReturnMessage::successToAppPage($res);
	}

	/**
	 * @param Request $request
	 * @return \Dingo\Api\Http\Response
	 * @name 打卡详情
	 * @user IHammer
	 */
	public static function clocksInfo(Request $request){
		$res = ClocksService::clocksInfoHandle($request);
		return ReturnMessage::successToAppInfo($res);
	}
	/**
	 * 打卡
	 * @author yuxingkui
	 * @param $request
	 * @return mixed
	 */
	public function clock( Request $request )
	{
		$input = ClockValidator::clock($request);
        $time = date('Y-m-d H:i:s',time());
        $user_id = constant("_USER_ID");
        $company_id = constant("_USER_CORPORATION_ID");
        $data['time'] = date('H:i:s',time());
		$count = count(Clock::isClock($input));
		if ( $count < 1 ){
			$input['status'] = 1;
			$data['status'] = 1;
			$input['online_time'] = $time;
			Clock::create($input);  //记录上班打卡时间
			$info = '上班打卡成功';
		}else{
		    if ($this->isOffline($user_id,$company_id) == 2)
            {
                $data['status'] = 3;
                $info =  '你已经下班打卡，不要重复操作。';
                return ReturnMessage::successData($data,$info);
            }
		    $where = [
		        'user_id' => $user_id,
                'company_id' => $company_id,
                'status' => 1,
            ];
			$content = [
			    'status' => 2,
                'offline_time' => $time,
            ];
			Clock::updateById($where,$content); //更新下班打卡时间
			$data['status'] = 2;
			$info = '下班打卡成功';

		}
		return ReturnMessage::successData($data,$info);
	}

    /**
     * 判断当前用户的打卡状态
     * @param $user_id
     * @param $company_id
	 *
     * @return mixed
     */
	private function isOffline($user_id,$company_id)
    {
        $where = [
            'user_id' => $user_id,
            'company_id' => $company_id,
        ];
        $info = Clock::getInfo($where,['status']);
        return reset($info)['status'];
    }

	/**
	 * @param Request $request
	 * @return \Dingo\Api\Http\Response
	 * @name 查询员工打卡记录
	 * @author liying
	 */
	public function card(Request $request)
	{
		$request->offsetSet('user_id',constant('_USER_ID'));
		$request->offsetSet('company_id',constant('_USER_CORPORATION_ID'));
		$request->offsetSet('user_role',constant('_USER_ROLE'));
		$res = CardService::showCardApi($request);
		if ($res)
			return ReturnMessage::successData($res);
		return ReturnMessage::success('查询失败',1002);
	}

	/**
	 * @param Request $request
	 * @return \Dingo\Api\Http\Response
	 * @name 打卡位置
	 * @author liying
	 */
	public function createGeo(Request $request)
	{
		$res = ClockValidator::create($request);
		$res = CardService::clockCreate($res);
		if ($res)
		{
			return ReturnMessage::successData(array('id'=>$res['id']));
		}
		return ReturnMessage::success('添加失败',1002);

	}

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author Liying
     * @name 查询企业配置信息
     */
	public function cardShow(Request $request){
        $company_id = constant('_USER_CORPORATION_ID');
        $res = CardService::getSettings($company_id);
        if ($res)
            return ReturnMessage::successData($res);
        return ReturnMessage::success('查询失败',1002);
    }

	/**
	 * @param Request $request
	 * @return \Dingo\Api\Http\Response
	 * @name 打卡设置
	 * @user IHammer
	 */
	public function clockSetting(Request $request){
		$request->offsetSet('user_id',constant('_USER_ID'));
		$request->offsetSet('company_id',constant('_USER_CORPORATION_ID'));
		$res = ClocksService::getClockSettingHandle($request);
		return ReturnMessage::successToAppInfo($res);
	}

}